package com.letwal.cmcvnd.extend

import android.annotation.SuppressLint
import androidx.core.text.isDigitsOnly
import com.letwal.cmcvnd.enums.CountryEnum
import com.letwal.cmcvnd.enums.CountryPathEnum
import com.letwal.cmcvnd.anetwork.ApiUrl
import com.letwal.cmcvnd.utils.UsageCountryUtil
import java.io.File
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern


fun String.toStringList(): List<String> {
    if (this.isBlank()) {
        return mutableListOf()
    }
    val list = this.split(",")
    val result = list.filter {
        it.isNotBlank()
    }
    return result
}

fun String.safeString(): String {
    if (this.isBlank()) {
        return ""
    }
    return this
}

fun String.safeInt(): Int {
    if (this.isBlank()) {
        return 0
    }
    if (this.isDigitsOnly()) {
        return this.toInt()
    }
    return 0
}

fun String.prefixWithAddUnit(): String {
    return ("+$this")
}

fun String.prefixWithMoneyUnit(): String {
    if (ApiUrl.BASE_PATH.contains(CountryEnum.NIGERIA.path, true)) {
        return (CountryEnum.NIGERIA.currency + " " + this.toFinanceString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.GHANA.path, true)) {
        return (CountryEnum.GHANA.currency + " " + this.toFinanceString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.KENYA.path, true)) {
        return (CountryEnum.KENYA.currency + " " + this.toFinanceString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.UGANDA.path, true)) {
        return (CountryEnum.UGANDA.currency + " " + this.toFinanceString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.COTEDIVA.path, true)) {
        return (CountryEnum.COTEDIVA.currency + " " + this.toFinanceString())
    }
    if (ApiUrl.BASE_PATH.contains(CountryEnum.VIETNAM.path, true)) {
        return (CountryEnum.VIETNAM.currency + " " + this.toFinanceString())
    }
    return (this.toFinanceString())
}

fun String.suffixWithMoneyUnit(): String {
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletnaira.name,
            true
        )
    ) {
        return (this.toFinanceString() + " ₦")
    }
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletgha.name,
            true
        )
    ) {
        return (this.toFinanceString() + " GHS")
    }
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletksh.name,
            true
        )
    ) {
        return (this.toFinanceString() + " KES")
    }
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletugx.name,
            true
        )
    ) {
        return (this.toFinanceString() + " UGX")
    }
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletfcfa.name,
            true
        )
    ) {
        return (this.toFinanceString() + " FCFA")
    }
    if (UsageCountryUtil.mApiPath.contains(
            CountryPathEnum.rawwalletvnd.name,
            true
        )
    ) {
        return (this.toFinanceString() + " VNĐ")
    }
    return (this.toFinanceString())
}

fun String.toDaysSuffix(): String {
    if (UsageCountryUtil.isCountryCotediva()) {
        return ("$this jours")
    }
    if (UsageCountryUtil.isCountryVietnam()) {
        return ("$this ngày")
    }
    return ("$this days")
}

fun String?.toFinanceString(): String {
    this?.let {
        if (it.isNotBlank()) {
            val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
            return resultDecimal.toPlainString()
        }
    }
    return ""
}

fun String?.toFinancePercentString(): String {
    if (this?.isNotBlank() == true) {
        try {
            val valueDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
            val edgeDecimal = BigDecimal(100)
            val resultDecimal: BigDecimal = valueDecimal.multiply(edgeDecimal)
            return ("${resultDecimal.stripTrailingZeros().toPlainString()}%")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return ""
}

fun String?.toAmountDouble(): Double {
    this?.let {
        if (it.isNotBlank()) {
            val resultDecimal: BigDecimal = BigDecimal(this)
            return resultDecimal.toDouble()
        }
    }
    return 0.0
}

fun String?.toEllipsisWalletAddress(): String {
    this?.let {
        if (it.length >= 20) {
            val start = it.substring(0, 10)
            val end = it.subSequence(it.length - 10, it.length)
            return ("$start...$end")
        }
    }
    return ""
}

fun String.toFinanceStringWithMoneyFlag(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toInterestRateString(): String {
    if (this.isNotBlank()) {
        try {
            val valueDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
            val edgeDecimal = BigDecimal(100)
            val resultDecimal: BigDecimal = valueDecimal.multiply(edgeDecimal)
            if (UsageCountryUtil.isCountryCotediva()) {
                return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per jour")
            }
            if (UsageCountryUtil.isCountryVietnam()) {
                return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per ngày")
            }
            return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per day")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return this
}

fun String.toEncryptPhoneNumber(): String {
    if (this.isNotBlank()) {
        val firstLine = if (this.length >= 3) this.substring(0, 3) else this
        val lastLine = if (this.length >= 6) this.substring(this.length - 3) else ""
        return "$firstLine **** $lastLine"
    }
    return this
}

fun String.toPhoneNumberInputHint(): String {
    if (this.isNotBlank()) {
        if (this.length >= 4) {
            var tempString = ""
            if (tempString.isBlank()) {
                tempString = this.substring(0, 4)
            }

            if (this.length >= 7) {
                tempString += " "
                tempString += this.substring(4, 7)
            }

            if (this.length >= 10) {
                tempString += " "
                tempString += this.substring(7)
            }
            return tempString

        } else {
            return this
        }
    }
    return this
}

fun String.filterForPhoneNumber(): String {
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    val m = p.matcher(this)
    return m.replaceAll("").trim()
}

fun String.isLink(): Boolean {
    return (this.safeString().startsWith("http"))
}

fun String.isEmailFormat(): Boolean {
    if (this.safeString().isNotBlank()) {
        return this.contains("@")
    }
    return false
}

fun String.suffixPath(): String {
    try {
        val file = File(this)
        return file.name
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return this
}

@SuppressLint("SimpleDateFormat")
fun String.dateForFormatString(targetFormat: String): Date? {
    try {
        val sDateFormat = SimpleDateFormat(targetFormat)
        val sdate = sDateFormat.parse(this)
        return sdate
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

fun String.toWords(): String {
    if (this.isBlank()) {
        return ""
    }

    var tempString = ""
    val wordList = this.split("_")
    try {
        for (index in 0 until wordList.size) {
            val word = wordList[index]
            if (word.isNotBlank()) {
                val capi = word.substring(0, 1).uppercase()
                val last = word.substring(1).lowercase()
                if (tempString.isBlank()) {
                    tempString = (capi + last)
                } else {
                    tempString = tempString + " " + (capi + last)
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return tempString
}
